//
//  PersonalInformationFormViewController.swift
//  iModern
//
//  Created by Ralf on 6/24/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit
import XLForm

class MyOrderformFormViewCellViewController: FormViewController
{
    var order      = NSDictionary()
    var payManager = PayTransactionManager()
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.initBackButton()
        self.initializeForm()
        self.initRightBarButtonItem()
        self.navigationItem.title = "订单详情"
    }
    
    func initializeForm()
    {
        let form:    XLFormDescriptor
        var section: XLFormSectionDescriptor
        var row:     XLFormRowDescriptor
        
        let orde        = order
        let advertising = orde["advertising"] as! [String:AnyObject]
        let contact     = advertising["contact"] as! [String:AnyObject]
        let review      = order["review"] as? String
        
        let price       = advertising["price"] as? Float
        let nub         = orde["quantity"] as? Float
        let totalPrices = price! * nub!
        
        form = XLFormDescriptor(title: "")
        section = XLFormSectionDescriptor.formSectionWithTitle("")
        form.addFormSection(section)
        
        if review == "null" || review == nil
        {
            row = XLFormRowDescriptor(tag: "evaluate", rowType: XLFormRowDescriptorTypeSelectorSegmentedControl, title: "未评价")
            row.selectorOptions = ["评价"]
            row.cellConfig["textLabel.color"] = UIColor.orangeColor()
            row.cellConfigAtConfigure["textLabel.color"] = UIColor.redColor()
            section.addFormRow(row)
        }
        else
        {
            row = XLFormRowDescriptor(tag: "evaluate", rowType: XLFormRowDescriptorTypeText, title: "已评价")
            row.cellConfig["textLabel.color"] = UIColor.orangeColor()
            row.cellConfigAtConfigure["textField.textAlignment"] = NSTextAlignment.Right.rawValue
            row.cellConfigAtConfigure["textLabel.color"] = UIColor.redColor()
            row.disabled = NSNumber(bool: true)
            section.addFormRow(row)
        }
        
        //var datas = self.order["contact"] as! NSDictionary
        row = XLFormRowDescriptor(tag: "alias", rowType: XLFormRowDescriptorTypeText, title: contact["fullName"] as? String)
        row.disabled = NSNumber(bool: true)
        row.cellConfigAtConfigure["textField.textAlignment"] = NSTextAlignment.Right.rawValue
        row.value = contact["shopName"] as? String
        row.required = true
        section.addFormRow(row)
        
        let p = String((orde["price"] as? Int)!)
        row = XLFormRowDescriptor(tag: "quantity", rowType: XLFormRowDescriptorTypeText, title: advertising["summary"] as? String)
        row.disabled = NSNumber(bool: true)
        row.cellConfigAtConfigure["textField.textAlignment"] = NSTextAlignment.Right.rawValue
        row.value = "￥" + p
        row.required = true
        section.addFormRow(row)
        
        let n = String((orde["quantity"] as? Int)!)
        row = XLFormRowDescriptor(tag: "totalPrices", rowType: XLFormRowDescriptorTypeText, title: "合计:")
        row.disabled = NSNumber(bool: true)
        row.cellConfigAtConfigure["textField.textAlignment"] = NSTextAlignment.Right.rawValue
        row.value = n + "个   " + "  ￥" + String(stringInterpolationSegment: totalPrices)
        row.required = true
        section.addFormRow(row)
        
        section = XLFormSectionDescriptor.formSectionWithTitle("订单详情")
        form.addFormSection(section)
        
        row = XLFormRowDescriptor(tag: "address", rowType: XLFormRowDescriptorTypeTextView, title: "快递地址:")
        row.disabled = NSNumber(bool: true)
        row.value = self.order["address"] as? String
        row.required = true
        section.addFormRow(row)
        
        let nameOfConsignee      = self.order["nameOfConsignee"] as? String
        let cellphoneOfConsignee = self.order["cellphoneOfConsignee"] as? String
        row = XLFormRowDescriptor(tag: "fullName", rowType: XLFormRowDescriptorTypeText, title: "收货人")
        row.disabled = NSNumber(bool: true)
        row.cellConfigAtConfigure["textField.textAlignment"] = NSTextAlignment.Right.rawValue
        row.value = nameOfConsignee! + "  " + cellphoneOfConsignee!
        row.required = true
        section.addFormRow(row)
        
        section = XLFormSectionDescriptor.formSectionWithTitle("订单留言")
        form.addFormSection(section)
        
        row = XLFormRowDescriptor(tag: "message", rowType: XLFormRowDescriptorTypeTextView, title: "")
        row.disabled = NSNumber(bool: true)
        row.value = self.order["message"] as? String
        row.required = true
        section.addFormRow(row)
        
        let id = self.order["id"] as! Int
        section = XLFormSectionDescriptor.formSectionWithTitle(" ")
        section.footerTitle = NSString(format: "订单号：%@", String(id)) as String
        form.addFormSection(section)
        
//        section = XLFormSectionDescriptor.formSectionWithTitle("联系iModern爱魔登客服： 4000060155")//attributeString
//        form.addFormSection(section)
    
   
        
        row = XLFormRowDescriptor(tag: "iModern", rowType: XLFormRowDescriptorTypeButton, title: "联系iModern爱魔登客服:4000060155")
        row.cellConfig["textLabel.color"] = UIColor.grayColor()
        row.cellConfig["textLabel.font"] = UIFont.systemFontOfSize(12)
        row.action.formSelector = "didTouchButton:"
        section.addFormRow(row)
        
        self.form = form
    }
    
    override func formRowDescriptorValueHasChanged(formRow: XLFormRowDescriptor!, oldValue: AnyObject!, newValue: AnyObject!)
    {
        super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
        
        if formRow.tag == "evaluate"
        {
            let totalPriceDescriptor                = self.form.formRowWithTag("evaluate")
            //let totalPriceCell: XLFormSegmentedCell = self.form.formRowWithTag("evaluate")?.cellForFormController(self) as! XLFormSegmentedCell
            if self.formValues()["evaluate"] as? String == "评价"
            {
                let controller = CommentViewController()
                controller.comment = order
                self.pushViewController(controller)
                totalPriceDescriptor?.value = ""
                
            }
            self.updateFormRow(totalPriceDescriptor)
        }
    }
    
    func initRightBarButtonItem()
    {
        let button: UIButton = UIButton(type: UIButtonType.System) ;
        button.frame = CGRectMake(0, 0, 80, 30)
        button.setTitle("联系卖家", forState: UIControlState.Normal)
        button.setTitleColor(UIColor(red: 0.40784313725490196, green: 0.6588235294117647, blue: 0.19215686274509805, alpha: 1), forState: UIControlState.Normal)
        button.addTarget(self, action: Selector("loadShareMenu:"), forControlEvents: UIControlEvents.TouchUpInside)
        let showButton = UIBarButtonItem(customView: button)
        self.navigationItem.rightBarButtonItem = showButton
    }
    
    @IBAction func loadShareMenu(sender: AnyObject)
    {
        let alertContorller = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
        let alertCancel = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
        let alertA = UIAlertAction(title: "私聊", style: UIAlertActionStyle.Default) {
            (action: UIAlertAction!) -> Void in
            self.takeChat()
        }
        let alertB = UIAlertAction(title: "电话", style: UIAlertActionStyle.Default) {
            (action: UIAlertAction!) -> Void in
            self.takePhone()
        }
        alertContorller.addAction(alertCancel)
        alertContorller.addAction(alertA)
        alertContorller.addAction(alertB)
        self.presentViewController(alertContorller, animated: true, completion: nil)
    }
    
    func takeChat()
    {
        let advertising = self.order["advertising"] as! [String:AnyObject]
        let contact     = advertising["contact"] as! [String:AnyObject]
        let controller  = ChatViewController()
        controller.contact = contact
        self.pushViewController(controller)
    }
    
    func takePhone()
    {
        let assignedTo = self.order["assignedTo"] as! [String:AnyObject]
        let cellphone = assignedTo["cellphone"] as? String
        let phone                = "tel://" + cellphone!
        UIApplication.sharedApplication().openURL(NSURL(string: phone)!)
    }
    
    func didTouchButton(sender: XLFormRowDescriptor)
    {
        let phone                = "tel://4000060155"
        UIApplication.sharedApplication().openURL(NSURL(string: phone)!)
        self.deselectFormRow(sender)
    }
    
    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
    // MARK: - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    }
    */
    
}
